import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';

import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { catchError, map, tap } from 'rxjs/operators';
import 'rxjs/add/operator/toPromise';
import { ClassMember, Member } from '../components/Interfaces/Member';
import { ClassService } from './class.service';
import { Search, ClassEntity } from '../components/Interfaces/ClassEntity';

const httpOptions = {
    headers: new Headers({
      'Content-Type': 'application/json',
    })
  };

@Injectable()
export class ArrangmentService {

    private apiUrl = 'api/v1/class_member';
    private classApiUrl = 'api/v1/class_member';
    constructor(
        private http: Http,
        private classService: ClassService
    ) {
    }

    /**
     * 添加成员
     */
    addClassMember(classMember: ClassMember): Observable<any> {
        return this.http.post(this.apiUrl, classMember, httpOptions).map(res => res.json());
    }

    /**
     * 从班级中移除成员
     */
    removeMemberFromClass(classMember: ClassMember): Observable<any> {
        return this.http.delete(this.apiUrl + '/' + classMember.id, httpOptions)
        .map(res => res.json());
    }

    removeMemberFromClass2(classEntity: ClassEntity, member: Member): Observable<any> {
        const delete_member_str = classEntity.id + '_' + member.id;
        return this.http.delete(this.apiUrl + '/' + delete_member_str , httpOptions)
        .map(res => res.json());
    }

    search(condition: Search): Observable<any> {
        return this.classService.getClassEntitiesByCondition(condition);
    }
}
